package com.kaifamiao.month04.day07;

public class Person {

    String name;
    String gender;
    int age;
    int id;
    String hobby;

    public boolean equals(Object obj) {
        //比较地址,如果地址相同，则引用相同。它们肯定相等
        if (this == obj) return true;
        // 如果为null，返回false
        if (obj == null)  return false;
        /*
         * 判断是否属于该类,包括其子类，根据需求来定如何判断
         *   如果说，两个必须是同一个类才能判断是否相等就用getClass(),要先判断null
         *   如果说，两个是同种类就能判断的话用 instanceof,可以不判断null
         */
        if (getClass() != obj.getClass())
            return false;
        //强制类型转换，转换成Person类
        Person other = (Person) obj;
        //逐个比较各个属性值是否相等
        if (age != other.age)
            return false;
        if (gender == null) {
            if (other.gender != null)
                return false;
        } else if (!gender.equals(other.gender))
            return false;
        if (hobby == null) {
            if (other.hobby != null)
                return false;
        } else if (!hobby.equals(other.hobby)) {
            return false;
        }
        if (name == null) {
            return other.name == null;
        } else return name.equals(other.name);
    }

    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((gender == null) ? 0 : gender.hashCode());
        result = prime * result + ((hobby == null) ? 0 : hobby.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
}
